import 'package:flutter_menu/data/obx_store.dart';
import 'package:objectbox/objectbox.dart';
import 'package:json_annotation/json_annotation.dart';

import '../objectbox.g.dart';
part 'formula_model.g.dart';

@Entity()
@JsonSerializable()
class FormulaModel with IEntity{
  @Id()
  int obxId;
  ///成本价
  double? costPrice;

  ///配方id
  String? formulaId;

  ///原料信息
  @JsonKey(fromJson: FormulaListFormulaMaterial.toManyFromJson, toJson: FormulaListFormulaMaterial.toManyJson)
  ToMany<FormulaListFormulaMaterial> formulaMaterials = ToMany<FormulaListFormulaMaterial>();

  ///菜品id
  String? productUuid;

  ///规格id
  String? specsUuid;

  FormulaModel({
    this.obxId =0,
    this.updateTime = 0,
     this.costPrice,
     this.formulaId,
     this.productUuid,
     this.specsUuid,
  });

  factory FormulaModel.fromJson(Map<String,dynamic> json) => _$FormulaModelFromJson(json);
  Map<String, dynamic> toJson() => _$FormulaModelToJson(this);

  static ToOne<FormulaModel> toOneFromJson(Map<String, dynamic> json) =>
  ToOne(target: FormulaModel.fromJson(json));

  static Map<String, dynamic>? toOneJson(ToOne<FormulaModel> s) =>
  s.target != null ? _$FormulaModelToJson(s.target!) : {};

  static ToMany<FormulaModel> toManyFromJson(List<dynamic>? json) =>
  ToMany(items: json?.map((e) => FormulaModel.fromJson(e)).toList());

  static List<Map<String, dynamic>>? toManyJson(ToMany<FormulaModel>? s) =>
  s?.toList().map((e) => e.toJson()).toList();

  @override
  int updateTime;

  @override
  int update(Store store, int? time) {
   updateTime = time?? updateTime;
   formulaMaterials.forEach((e)=> e.obxId = e.update(store,time));
   var box =  store.box<FormulaModel>();
   return box.update(this, FormulaModel_.formulaId, formulaId);
  }

}

@Entity()
@JsonSerializable()
class FormulaListFormulaMaterial  with IEntity{
  @Id()
  int obxId;

  ///原料配方关联id
  String? formulaMaterialId;

  ///原料id
  String? materialId;

  ///原料名
  String? materialName;

  ///原料/半成品第二语言
  String? materialSecondName;

  ///单位
  String? measureName;

  ///使用量
  double? usageAmount;

  FormulaListFormulaMaterial({
    this.obxId = 0,
    this.updateTime = 0,
    this.formulaMaterialId,
     this.materialId,
     this.materialName,
     this.materialSecondName,
     this.measureName,
     this.usageAmount,
  });

  factory FormulaListFormulaMaterial.fromJson(Map<String,dynamic> json) => _$FormulaListFormulaMaterialFromJson(json);
  Map<String, dynamic> toJson() => _$FormulaListFormulaMaterialToJson(this);

  static ToOne<FormulaListFormulaMaterial> toOneFromJson(Map<String, dynamic> json) =>
  ToOne(target: FormulaListFormulaMaterial.fromJson(json));

  static Map<String, dynamic>? toOneJson(ToOne<FormulaListFormulaMaterial> s) =>
  s.target != null ? _$FormulaListFormulaMaterialToJson(s.target!) : {};

  static ToMany<FormulaListFormulaMaterial> toManyFromJson(List<dynamic>? json) =>
  ToMany(items: json?.map((e) => FormulaListFormulaMaterial.fromJson(e)).toList());

  static List<Map<String, dynamic>>? toManyJson(ToMany<FormulaListFormulaMaterial>? s) =>
  s?.toList().map((e) => e.toJson()).toList();

  @override
  int updateTime;

  @override
  int update(Store store, int? time) {
    updateTime = time?? updateTime;
    var box =  store.box<FormulaListFormulaMaterial>();
    return box.put(this);
  }

}